package com.cqeec.view;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.LayoutStyle.ComponentPlacement;

import com.cqeec.domain.Manager;
import com.cqeec.domain.Student;
import com.cqeec.domain.Teacher;
import com.cqeec.service.ManagerService;
import com.cqeec.service.StudentService;
import com.cqeec.service.TeacherService;
import com.cqeec.service.imp.ManagerServicelmpl;
import com.cqeec.service.imp.StudentServiceImpl;
import com.cqeec.service.imp.TeacherServiceImpl;
import com.cqeec.utils.JDBCUtils;
/**
 * 修改密码界面
 * @author 邓学强
 *  修改密码 需要提供旧密码
 */
public class ModifyPassword extends JInternalFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPasswordField passwordField;
	private JPasswordField passwordField_1;
	private JPasswordField passwordField_2; 
	private Object obj;
	/**
	 * Create the frame.
	 */
	public ModifyPassword(Object obj) {
		this.obj = obj;
		setTitle("修改密码");
		setIconifiable(true);
		setClosable(true);
		setBounds(100, 100, 527, 460);
		
		JLabel pastpassword = new JLabel("旧密码：");
		pastpassword.setFont(new Font("宋体", Font.BOLD, 20));
		
		passwordField = new JPasswordField();
		
		JLabel label = new JLabel("新密码：");
		label.setFont(new Font("宋体", Font.BOLD, 20));
		
		passwordField_1 = new JPasswordField();
		
		JLabel label_1 = new JLabel("确认密码：");
		label_1.setFont(new Font("宋体", Font.BOLD, 20));
		
		passwordField_2 = new JPasswordField();
		
		JButton button = new JButton("修改");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String password = new String(passwordField.getPassword(), 0, passwordField.getPassword().length);
				String newPassword = new String(passwordField_1.getPassword(), 0, passwordField_1.getPassword().length);
				String verificationPassword = new String(passwordField_2.getPassword(), 0,
						passwordField_2.getPassword().length);
				// 更新密码 验证
				if (!newPassword.equals(verificationPassword)) {
					JOptionPane.showMessageDialog(null, "新密码错误两次不一致", "提示", JOptionPane.CLOSED_OPTION);
					return;
				}

				if (obj instanceof Teacher) {
					Teacher teacher = (Teacher) obj;

					if (!password.equals(teacher.getTeacherPwd())) {
						JOptionPane.showMessageDialog(null, "密码错误", "提示", JOptionPane.CLOSED_OPTION);
						return;
					}

					teacher.setTeacherPwd(newPassword);

					TeacherService service = new TeacherServiceImpl();

					service.upateTeacherByTeacherNum(JDBCUtils.getConnection(), teacher);
					return;
				}

				if (obj instanceof Manager) {
					Manager manager = (Manager) obj;
					if (!password.equals(manager.getManagerPwd())) {
						JOptionPane.showMessageDialog(null, "密码错误", "提示", JOptionPane.CLOSED_OPTION);
						return;
					}

					ManagerService service = new ManagerServicelmpl();

					manager.setManagerPwd(newPassword);

					try {
						service.upateManagerByManagerName(JDBCUtils.getConnection(), manager);
					} catch (Exception e1) {
						e1.printStackTrace();
					}

					return;

				}

				if (obj instanceof Student) {
					Student student = (Student) obj;
					if (!password.equals(student.getStudentPwd())) {
						JOptionPane.showMessageDialog(null, "密码错误", "提示", JOptionPane.CLOSED_OPTION);
						return;
					}
					StudentService service = new StudentServiceImpl();

					student.setStudentPwd(newPassword);

					try {
						service.upateStudentByStudentName(JDBCUtils.getConnection(), student);
					} catch (Exception e1) {
						e1.printStackTrace();
					}

				}
				
			}
		});
		button.setFont(new Font("宋体", Font.BOLD, 20));
		/**
		 * 重置代码事件
		 */
		JButton button_1 = new JButton("重置");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) { 
				passwordField.setText("");
				passwordField_1.setText("");
				passwordField_2.setText("");
			}
		});
		button_1.setFont(new Font("宋体", Font.BOLD, 20));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addGroup(groupLayout.createSequentialGroup()
							.addContainerGap()
							.addComponent(button)
							.addGap(75)
							.addComponent(button_1))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(107)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addComponent(label_1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(label, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(pastpassword, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, 160, GroupLayout.PREFERRED_SIZE)
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
									.addComponent(passwordField_1, GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE)
									.addComponent(passwordField_2, GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE)))))
					.addGap(141))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(90)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(pastpassword)
						.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE))
					.addGap(34)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(label)
						.addComponent(passwordField_1, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE))
					.addGap(41)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(label_1)
						.addComponent(passwordField_2, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, 71, Short.MAX_VALUE)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(button_1)
						.addComponent(button))
					.addGap(60))
		);
		getContentPane().setLayout(groupLayout);
		setVisible(true);
	}

	 
}
